/*
*功能：ESP32连接MQTT
*作者：刘泽文
*/

#include <Arduino.h>
#include <WiFi.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <U8g2lib.h>
#include <Wire.h>

#define LED 2

const char *ssid = "liuzewen"; // Enter your WiFi name
const char *password = "17609245102liu";  // Enter WiFi password

// MQTT Broker
const char *mqtt_broker = "192.168.31.45";//ipconfig:18083
const char *topic = "esp32/led";
const char *mqtt_username = "admin";
const char *mqtt_password = "public";
const int mqtt_port = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

void callback(char *topic, byte *payload, unsigned int length);
void u8g2_faution(bool led_state);

unsigned long back_time = 0;

void setup() {
    // Set software serial baud to 115200;
    Serial.begin(115200);

    u8g2.begin();
    u8g2.enableUTF8Print();
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_wqy16_t_gb2312);
    u8g2.setCursor(34, 40);
    u8g2.print("本地MQTT");
    u8g2.sendBuffer();

    pinMode(LED,OUTPUT);
    digitalWrite(LED,LOW);

   // connecting to a WiFi network
   WiFi.begin(ssid, password);
   while (WiFi.status() != WL_CONNECTED) {
       delay(500);
       Serial.println("Connecting to WiFi..");
   }
   Serial.println("Connected to the WiFi network");
   //connecting to a mqtt broker
   client.setServer(mqtt_broker, mqtt_port);
   client.setCallback(callback);
   while (!client.connected()) {
       String client_id = "esp32-client-";
       client_id += String(WiFi.macAddress());
       Serial.println("Connecting to public emqx mqtt broker.....");
       if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
           Serial.println("Public emqx mqtt broker connected");
       } else {
           Serial.print("failed with state ");
           Serial.print(client.state());
           delay(2000);
       }
   }
   // publish and subscribe
   client.publish(topic, "hello emqx");
   client.subscribe(topic);
}

void loop() {
   client.loop();
}

void callback(char *topic, byte *payload, unsigned int length) {
   Serial.print("Message arrived in topic: ");
   Serial.println(topic);
   Serial.print("Message:");
   String message;
   for (int i = 0; i < length; i++) {
       message = message + (char) payload[i];  // convert *byte to string
   }
   Serial.print(message);
   if (message == "1") {
       digitalWrite(LED, HIGH);
       u8g2_faution(1);
    }   // LED on
   if (message == "0") {
       digitalWrite(LED, LOW);
       u8g2_faution(0);
    } // LED off
   Serial.println();
   Serial.println("-----------------------");
}

void u8g2_faution(bool led_state)
{
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_wqy16_t_gb2312);
    u8g2.setCursor(0, 16);
    u8g2.print("   MQTT测试   ");//一行共8个汉字
    u8g2.setCursor(10, 55);
    u8g2.print("灯1状态：");
    u8g2.setFont(u8g2_font_open_iconic_all_4x_t);
    if(led_state)
      u8g2.drawGlyph(85,64,120);//开灯
    else
      u8g2.drawGlyph(85,64,121);//关灯
    u8g2.sendBuffer();
}